package com.emip.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.emip.bean.ExpFlagBean;
import com.emip.dao.BaseDAO;
import com.emip.dao.ExpFlagDAO;

@Repository("expFlagDAO")
public class ExpFlagDAOImpl extends BaseDAO implements ExpFlagDAO {

	public int add(ExpFlagBean entity) {
		Integer id = (Integer)this.getHibernateTemplate().save(entity);
		return id.intValue();
	}

	public ExpFlagBean findByID(Integer ID) {
		ExpFlagBean bean = this.getHibernateTemplate().get(ExpFlagBean.class, ID);
		return bean;
	}

	public Boolean findExist(ExpFlagBean entity) {
		Object[] params = new Object[] {entity.getFlag()};
		List<ExpFlagBean> result = this.getHibernateTemplate().find("from ExpFlagBean m where flag=?", params);
		if (result.size()==0) {
			return false;
		}
		else {
			//是否存在的那条记录的flag和自己是相同的，如果是的话，那么其实是update其他字段的一种操作，也是合法操作
			if (entity.getId()==0) {  //新增的情况
				return true;
			} else {
				ExpFlagBean eBean = this.getHibernateTemplate().get(ExpFlagBean.class, entity.getId());
				if (entity.getFlag().equals(eBean.getFlag()) ) {
					return false;
				} else {
					return true;
				}
			}
		}
	}

	public List<ExpFlagBean> getAll() {
		List<ExpFlagBean> result = this.getHibernateTemplate().find("from ExpFlagBean m order by m.id");
		return result;
	}

	public void remove(ExpFlagBean entity) {
		this.getHibernateTemplate().delete(entity);
	}

	public void update(ExpFlagBean entity) {
		this.getHibernateTemplate().update(entity);
	}

}
